package com.thl.demo;


import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.thl.filechooser.FileChooser;
import com.thl.filechooser.FileInfo;
import com.thl.filechooser.FileUriUtils;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.documentfile.provider.DocumentFile;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String[] strings = {Permission.MANAGE_EXTERNAL_STORAGE};
        XXPermissions.with(this)
                // 申请安装包权限
                //.permission(Permission.REQUEST_INSTALL_PACKAGES)
                // 申请悬浮窗权限
                //.permission(Permission.SYSTEM_ALERT_WINDOW)
                // 申请通知栏权限
                //.permission(Permission.NOTIFICATION_SERVICE)
                // 申请系统设置权限
                //.permission(Permission.WRITE_SETTINGS)
                // 申请单个权限
//                .permission(Permission.RECORD_AUDIO)
                // 申请多个权限
                .permission(strings)
                .request(new OnPermissionCallback() {

                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                    }
                });


//        requestPermissins(new PermissionUtils.OnPermissionListener() {
//            @Override
//            public void onPermissionGranted() {
//
//            }
//
//            @Override
//            public void onPermissionDenied(String[] deniedPermissions) {
//                Toast.makeText(MainActivity.this, "未获取到存储权限", Toast.LENGTH_SHORT).show();
//            }
//        });

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FileChooser fileChooser = new FileChooser(MainActivity.this, new FileChooser.FileChoosenListener() {
                    @Override
                    public void onFileChoosen(List<FileInfo> filePaths) {
//                                ((TextView) findViewById(R.id.tv_msg)).setText(filePaths.get(0).getFilePath());
                        for (FileInfo filePath : filePaths) {
                            Log.e("惨哦慢你的刘嘉玲>>>>>>>",filePath.getFilePath());
                        }
                    }
                });
                String path = FileUriUtils.root+"Android/data";
                fileChooser.setShowHideFile(false);
                fileChooser.showFile(true);
                fileChooser.setChooseType(FileInfo.FILE_TYPE_ALL);
                fileChooser.setChooseCount(199999);
                fileChooser.showExternalSdCard(false);
                fileChooser.setCurrentPath(path);
                fileChooser.open();
            }
        });

        findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                DocumentFile documentFile = FileUriUtils.getDoucmentFile(MainActivity.this,FileUriUtils.mVideoPath);
                documentFile.renameTo("/storage/emulated/0/video2.m4s");

            }
        });
//
//        findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                requestPermissins(new PermissionUtils.OnPermissionListener() {
//                    @Override
//                    public void onPermissionGranted() {
//                        FileChooser fileChooser = new FileChooser(MainActivity.this, new FileChooser.FileChoosenListener() {
//                            @Override
//                            public void onFileChoosen(String filePath) {
//                                ((TextView) findViewById(R.id.tv_msg)).setText(filePath);
//                            }
//                        });
//
////                          FILE_TYPE_FOLDER="type_folder";  //文件夹
////                          FILE_TYPE_VIDEO="type_video";    //视频
////                          FILE_TYPE_AUDIO="type_audio";    //音频
////                          FILE_TYPE_FILE="type_file";      //全部文件
////                          FILE_TYPE_APK="type_apk";        //apk
////                          FILE_TYPE_ZIP="type_zip";        //zip
////                          FILE_TYPE_RAR="type_rar";        //rar
////                          FILE_TYPE_JPEG="type_jpeg";      //jpeg
////                          FILE_TYPE_JPG="type_jpg";         //jpg
////                          FILE_TYPE_PNG="type_png";         //png
////
////                          FILE_TYPE_ALL="type_all";         //所有文件
////                           FILE_TYPE_IMAGE="type_image";    //所有图片
////                           FILE_TYPE_PACKAGE="type_package";  //压缩包
//
//                        fileChooser.setChooseType(FileInfo.FILE_TYPE_VIDEO);
//                        fileChooser.open();
//                    }
//
//                    @Override
//                    public void onPermissionDenied(String[] deniedPermissions) {
//                        Toast.makeText(MainActivity.this, "未获取到存储权限", Toast.LENGTH_SHORT).show();
//                    }
//                });
//
//
//            }
//        });
//
//        findViewById(R.id.button4).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                requestPermissins(new PermissionUtils.OnPermissionListener() {
//                    @Override
//                    public void onPermissionGranted() {
//                        FileChooser fileChooser = new FileChooser(MainActivity.this, new FileChooser.FileChoosenListener() {
//                            @Override
//                            public void onFileChoosen(String filePath) {
//                                ((TextView) findViewById(R.id.tv_msg)).setText(filePath);
//                            }
//                        });
//                        fileChooser.setCurrentPath(MainActivity.this.getExternalCacheDir().getAbsolutePath());
//                        fileChooser.showFile(false); //不显示文件
//                        fileChooser.open();
//                    }
//
//                    @Override
//                    public void onPermissionDenied(String[] deniedPermissions) {
//                        Toast.makeText(MainActivity.this, "未获取到存储权限", Toast.LENGTH_SHORT).show();
//                    }
//                });
//
//
//            }
//        });

    }

    private void requestPermissins(PermissionUtils.OnPermissionListener mOnPermissionListener) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            mOnPermissionListener.onPermissionGranted();
            return;
        }
        String[] permissions = { "android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE","android.permission.MANAGE_EXTERNAL_STORAGE"};
        PermissionUtils.requestPermissions(this, 0
                , permissions, mOnPermissionListener);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionUtils.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }




}
